<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Namespaces in C#</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="C#, learn C#, namespace, .NET, Mono, tutorial, programming language">
<meta name="description" content="This part of the C# tutorial covers namespaces.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>



<h1>Namespaces</h1>

<p>
In this part of the C# tutorial, we will describe namespaces. 
</p>


<p>
Namespaces are used to organize code at the highest logical level. 
They classify and present programming elements that are exposed 
to other programs and applications. Within a namespace, we can 
declare another namespace, a class, an interface, a struct, an enum
or a delegate. We cannot define items such as properties, variables 
and events. These items must be declared within containers such as 
structures or classes. Namespaces prevent ambiguity and simplify references 
when using large groups of objects such as class libraries.
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

<p>
Namespaces organize objects in an assembly. An assembly is a reusable,
versionable and self-describing building block of a CLR application.
Assemblies can contain multiple namespaces. Namespaces can contain other
namespaces. An assembly provides a fundamental unit of physical code grouping.
A namespace provides a fundamental unit of logical code grouping.
</p>


<pre class="code">
public class CSharpApp
{
    static void Main()
    {
        System.Console.WriteLine("Simple namespace example");
    }
}
</pre>

<p>
The built-in libraries are organized within namespaces. Take the Console
class. It is available within the System namespace. To call the static 
WriteLine() method of the Console class, we use its fully qualified name.
Fully qualified names are object references that are prefixed with the 
name of the namespace where the object is defined.
</p>

<hr class="btm">

<p>
In the following code, we have two files that share the same namespace. 
</p>

<pre class="code">
using System;

// namespace2.cs

namespace ZetCode
{
    public class Example
    {
        public int x = 0;
        
        public void Raise()
        {
            x += 100;
            Console.WriteLine(x);
        } 
    }
}
</pre>

<p>
We have a ZetCode namespace. In the namespace, we have a class
Example. 
</p>

<pre class="explanation">
namespace ZetCode
{
...
}
</pre>

<p>
We declare a namespace called ZetCode. The code goes inside the curly
brackets of the ZetCode namespace.
</p>


<pre class="code">
// namespace1.cs

namespace ZetCode
{
    public class CSharpApp
    {
        static void Main()
        {
            Example ex = new Example();
            ex.Raise();
            ex.Raise();
        }
    }
}
</pre>

<p>
In the second file, we work with the Example class from the previous file. 
We invoke its Raise() method 
</p>

<pre class="explanation">
namespace ZetCode
</pre>

<p>
We work in the same namespace.
</p>

<pre class="explanation">
Example ex = new Example();
ex.Raise();
ex.Raise();
</pre>

<p>
We create the instance of the Example class. We call its Raise() method 
twice. Because we work with objects of the same namespace, we do not
need to specify its name. 
</p>

<pre>
$ gmcs namespace1.cs namespace2.cs
$ ./namespace1.exe 
100
200
</pre>

<p>
Output. 
</p>

<hr class="btm">

<p>
The following code example has two distinct namespaces.
We use the <code>using</code> keyword to import elements from 
a different namespace. 
</p>

<pre class="code">
// distinctnamespace2.cs

namespace MyMath
{
    public class Basic
    {
        public static double PI = 3.141592653589;
        
        public static double GetPi()
        {
            return PI;
        } 
    }
}
</pre>

<p>
We have a skeleton of a Math class in a MyMath namespace.
In the Basic class, we define a PI constant and a GetPi() 
method.
</p>

<pre class="code">
// distinctnamespace1.cs

using MyMath;
using System;

namespace ZetCode
{
    public class CSharpApp
    {
        static void Main()
        {
            Console.WriteLine(Basic.PI);
            Console.WriteLine(Basic.GetPi());
        }
    }
}
</pre>

<p>
In this file, we use the elements from the MyMath
namespace. 
</p>

<pre class="explanation">
using MyMath;
</pre>

<p>
We import the elements from the MyMath namespace into our
namespace. 
</p>

<pre class="explanation">
Console.WriteLine(Basic.PI)
Console.WriteLine(Basic.GetPI())
</pre>

<p>
Now we can use those elements. In our case it is the Basic class.
</p>

<pre>
$ gmcs distinctnamespace1.cs distinctnamespace2.cs
$ ./distinctnamespace1.exe 
3.141592653589
3.141592653589
</pre>

<p>
We compile the two files and run the program. 
</p>


<p>
This part of the C# tutorial was dedicated to namespaces.
</p>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified November 1, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

